toPositiveIntOrThrow

@ExperimentalSinceKotoolsTypes(version = "4.3.1")
fun Number.toPositiveIntOrThrow(): PositiveInt

Returns this number as a PositiveInt, which may involve rounding or truncation, or throws IllegalArgumentException if this number is strictly negative.

var result: PositiveInt = 1.toPositiveIntOrThrow()
println(result) // 1

result = 0.toPositiveIntOrThrow()
println(result) // 0

(-1).toPositiveIntOrThrow() // IllegalArgumentException

You can use the toPositiveIntOrNull function for returning null instead of throwing an IllegalArgumentException when this number is strictly negative.